package com.bawan.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Desc: 数据库JDBC连接工具类
 * User: ZhengCongChun
 * Date: 14-2-20
 * Time: 上午9:59
 *
 * @Version 1.0
 */
public class DBConnUtil {
    private static Log log = LogFactory.getLog(DBConnUtil.class);

    private static DBConnUtil instance = null;
    public static synchronized DBConnUtil getInstance() {
        if ((instance != null) && (instance instanceof DBConnUtil)) {
            return instance;
        }
        return instance = new DBConnUtil();
    }

    /**
     * 根据配置文件的参数打开数据库连接
     * @return
     */
    public Connection openConnection() {
        String url = "";
        String driver = "";
        String username = "";
        String password = "";

        Properties props = new Properties();

        try {
            props.load(this.getClass().getClassLoader().getResourceAsStream("sysConfig.properties"));

            driver = props.getProperty("jdbc.driver");
            url = props.getProperty("jdbc.url");
            username = props.getProperty("jdbc.username");
            password = props.getProperty("jdbc.password");

            Class.forName(driver);

            Connection conn = DriverManager.getConnection(url, username, password);

            return conn;
        } catch (Exception e){
//            e.printStackTrace();
            log.error("获取数据库连接失败。错误信息：" + e.getMessage());
        }

        return null;
    }

    /**
     * 根据传入的参数获得数据库连接
     * @param driver    数据库驱动类
     * @param url   数据库连接URL
     * @param username 用户名
     * @param password  密码
     * @return
     */
    public Connection getConnection(String driver, String url, String username, String password) {
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, username, password);

            return conn;
        } catch (Exception e) {
//            e.printStackTrace();
            log.error("获取数据库连接失败。错误信息：" + e.getMessage());
        }

        return null;
    }

    /**
     * 获得MySQL数据库连接
     * @return
     */
    public Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db", "root", "root");
            return conn;
        } catch (Exception e) {
//            e.printStackTrace();
            log.error("获取数据库连接失败。错误信息：" + e.getMessage());
        }
        return null;
    }

    /**
     * 关闭一个数据库连接
     * @param conn
     */
    public void closeConnection(Connection conn) {
        try {
            if ((conn != null) && (conn instanceof Connection))
                conn.close();
        } catch (SQLException e) {
//            e.printStackTrace();
            log.error("关闭数据库连接失败。错误信息：" + e.getMessage());
        }

    }
}
